package com.linq.cool.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.PageVO;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.log.annotation.Log;
import com.linq.cool.common.log.enums.BusinessType;
import com.linq.cool.common.security.annotation.PreAuthorize;
import com.linq.cool.system.domain.SysPost;
import com.linq.cool.system.dto.SysPostQueryDTO;
import com.linq.cool.system.service.SysPostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2021/1/25 10:07
 * @Description:
 * @Version: 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("post")
public class SysPostController extends BaseController {
    @Autowired
    private SysPostService sysPostService;

    /**
     * 条件分页获取岗位列表
     *
     * @param dto 岗位信息
     *
     * @return 分页数据集合
     */
    @PreAuthorize(hasPerm = "sys:post:list")
    @GetMapping("list")
    public Result<?> list(SysPostQueryDTO dto) {
        log.info("SysPostQueryDTO: {}", dto);
        IPage<SysPost> page = sysPostService.listByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize(hasPerm = "sys:post:query")
    @GetMapping(value = "/{postId}")
    public Result<?> getInfo(@PathVariable Long postId) {
        return Result.ok(sysPostService.getById(postId));
    }

    /**
     * 新增岗位
     */
    @Log(moduleTitle = "岗位管理", businessType = BusinessType.INSERT)
    @PreAuthorize(hasPerm = "sys:post:add")
    @PostMapping
    public Result<?> add(@Validated @RequestBody SysPost post) {
        return this.toResult(sysPostService.savePost(post));
    }

    /**
     * 修改岗位
     */
    @Log(moduleTitle = "岗位管理", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "sys:post:edit")
    @PutMapping
    public Result<?> edit(@Validated @RequestBody SysPost post) {
        return this.toResult(sysPostService.updatePost(post));
    }

    /**
     * 删除岗位
     */
    @Log(moduleTitle = "岗位管理", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPerm = "sys:post:remove")
    @DeleteMapping("/{postIds}")
    public Result<?> remove(@PathVariable List<Long> postIds) {
        return this.toResult(sysPostService.deleteByIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/getOptionSelect")
    public Result<?> getOptionSelect() {
        return Result.ok(sysPostService.list());
    }
}
